package manager.cms.service.utils;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
 * 网络爬虫工具类
 */
public class JdInternetWormUtils {
    //生成规格参数模板字符串，以及每一个商品所需要的规格参数JSON字符串
    public static Map<String,Object> getTemplete(String itemHtml){
            //获取所有没有分组规格参数,单独与分组放在同一级
            List<Map<String,Object>> paramsListTemplete=new ArrayList<Map<String,Object>>();
            List<Map<String,Object>> paramsListData=new ArrayList<Map<String,Object>>();
            //获取所有分组的规格参数,里面存入的是组+加上规格参数列表
            List<Map<String,Object>> groupParamsListTemplete=new ArrayList<Map<String,Object>>();
            List<Map<String,Object>> groupParamsListData=new ArrayList<Map<String,Object>>();
            Map<String,Object> params=new HashMap<String,Object>();
            Map<String,Object> paramsMap= JdInternetWormUtils.asyncParams(itemHtml);
            Map<String,Object> paramsGroupMap=JdInternetWormUtils.asyncParamsGroup(itemHtml);
            List<Map<String,Object>> specialParamsTemp=(List<Map<String,Object>>)JdInternetWormUtils.getSpecTemplete(itemHtml,false).get("specialParams");
            String brandPattern="id=\"parameter-brand\"[\\s\\S]*?</ul>";
            String brandStr=JdInternetWormUtils.getStrArrayByReg(itemHtml,brandPattern).get(0);
            String brandName=JdInternetWormUtils.getStrArrayByReg(brandStr,"'[\\s\\S]*?>品").get(0);
            brandName=brandName.substring(1,brandName.length()-3);
            paramsMap.put("品牌",brandName);
            paramsListTemplete=buildParam(paramsMap,false);
            paramsListData=buildParam(paramsMap,true);
            if(specialParamsTemp!=null){
                paramsListTemplete.addAll(specialParamsTemp);
                paramsListData.addAll(specialParamsTemp);
            }
            Map<String,Object> paramsResult=new HashMap<String,Object>();
            Map<String,Object> paramsResultData=new HashMap<String,Object>();
            paramsResult.put("params",paramsListTemplete);
            paramsResultData.put("params",paramsListData);
            paramsGroupMap.forEach((k,v)->{
                 Map<String,Object> groupItem=new HashMap<String,Object>();
                 Map<String,Object> paramItemMap=(Map<String,Object>)v;
                 groupItem.put("group",k);
                 groupItem.put("params",buildParam(paramItemMap,false));
                 groupParamsListTemplete.add(groupItem);
                 Map<String,Object> groupItemData=new HashMap<String,Object>();
                 groupItemData.put("group",k);
                 groupItemData.put("params",buildParam(paramItemMap,true));
                 groupParamsListData.add(groupItemData);
            });
            paramsResult.put("groupParams",groupParamsListTemplete);
            paramsResultData.put("groupParams",groupParamsListData);
            String paramsTempStr=new Gson().toJson(paramsResult);
            String paramsDataStr=new Gson().toJson(paramsResultData);
            params.put("paramsTempStr",paramsTempStr);
            params.put("paramsDataStr",paramsDataStr);
            params.put("paramsTemp",paramsResult);
            params.put("paramsData",paramsResultData);
            return params;
    }
    public static List<Map<String,Object>> buildParam(Map<String,Object> orgData,Boolean isNeedV){
            List<Map<String,Object>> paramItems=new ArrayList<Map<String,Object>>();
            orgData.forEach((k,v)->{
                Map<String,Object> mapItem=new HashMap<String,Object>();
                mapItem.put("generic",true);
                mapItem.put("searching",false);
                mapItem.put("segments",false);
                mapItem.put("k",k);
                if(isNeedV){
                    mapItem.put("v",v);
                }
                mapItem.put("numberic",false);
                paramItems.add(mapItem);
            });
            return paramItems;
    }
    //生成规格参数模板需要的json字符串
    public static Map<String,Object> getSpecTemplete(String itemHtml,Boolean isNeedV){
            List<Map<String,Object>> params=new ArrayList<Map<String,Object>>();
            Map<String,Object> map=new HashMap<String,Object>();
            JSONArray skuArray= null;
           try {
                String pattern = "colorSize.*?]";
                List<String> skuStrList=JdInternetWormUtils.getStrArrayByReg(itemHtml,pattern);
                if(skuStrList.size()==0){
                    return map;
                }
                String skusStr=skuStrList.get(0);
                skusStr=skusStr.substring(11,skusStr.length());
                skuArray = new JSONArray(skusStr);
                JSONObject skuObjFirst=(JSONObject)skuArray.get(0);
                Iterator<String> iterator = skuObjFirst.keys();
                while (iterator.hasNext()){
                    Map<String,Object> mapItem=new HashMap<String,Object>();
                    String key=iterator.next();
                    if(key.equals("skuId")){
                        continue;
                    }
                    //需要过滤数据
                    List<Map<String,Object>> optionsObjList=new ArrayList<Map<String,Object>>();
                    List<String> optionsList=new ArrayList<String>();
                    for (int j=0;j<skuArray.length();j++) {
                        JSONObject skuObj = (JSONObject) skuArray.get(j);
                        optionsList.add(skuObj.get(key).toString());
                    }
                    int index=0;
                    optionsList=optionsList.stream().distinct().collect(Collectors.toList());
                    for (int k=0;k<optionsList.size();k++){
                        Map<String,Object> opMap=new HashMap<String,Object>();
                        opMap.put("index",index++);
                        opMap.put("v",optionsList.get(k));
                        optionsObjList.add(opMap);
                    }
                    //组装特殊规格参数
                    mapItem.put("generic",false);
                    mapItem.put("searching",false);
                    mapItem.put("segments",false);
                    mapItem.put("k",key);
                    if(isNeedV){
                        mapItem.put("v",skuObjFirst.get(key));
                    }
                    mapItem.put("numberic",false);
                    mapItem.put("options",optionsObjList);
                    params.add(mapItem);
                }
            } catch (JSONException e) {

            }
            map.put("specialParams",params);
            map.put("skuArray",skuArray);
            return  map;
    }
    //解析规格参数
    public static Map<String,Object> asyncParams(String itemHtml){
            Map<String,Object> map=new HashMap<String,Object>();
            String pattern = "\"parameter2 p-parameter-list\">[\\S\\s]*?</ul>";
            List<String> paramsStrList=getStrArrayByReg(itemHtml,pattern);
            pattern = "'>[\\S\\s]*?</li>";
            List<String> paramsList=getStrArrayByReg(paramsStrList.get(0),pattern);
            for(String spuStr:paramsList){
                spuStr=spuStr.substring(2,spuStr.length()-5);
                String[] paramArray=spuStr.split("：");
                map.put(paramArray[0],paramArray[1]);
            }
            return map;
    }
    //同步规格参数组以及规格参数
    public static Map<String,Object> asyncParamsGroup(String itemHtml){
            itemHtml=itemHtml.replaceAll("\"Ptable-tips\">[\\S\\s]*?</dd>","");
            Map<String,Object> map=new HashMap<String,Object>();
            String pattern = "\"Ptable-item\">[\\S\\s]*?</div>";
            List<String> groupStrList=getStrArrayByReg(itemHtml,pattern);
            for(String groupStr:groupStrList){
                pattern = "<h3>[\\S\\s]*?</h3>";
                List<String> groupKeyList=getStrArrayByReg(groupStr,pattern);
                Map<String,Object> paramsMap=new HashMap<String,Object>();
                map.put(groupKeyList.get(0).substring(4,groupKeyList.get(0).length()-5),paramsMap);
                pattern = "<dl class[\\S\\s]*?</dl>";
                List<String> htmlContent=getStrArrayByReg(groupStr,pattern);
                for(String content:htmlContent){
                    pattern = "<dt>[\\S\\s]*?</dt>";
                    List<String> paramKeyList=getStrArrayByReg(content,pattern);
                    pattern = "<dd>[\\S\\s]*?</dd>";
                    List<String> paramValueList=getStrArrayByReg(content,pattern);
                    if(paramKeyList.size()>0&&paramValueList.size()>0){
                        paramsMap.put(paramKeyList.get(0).replace("<dt>","").replace("</dt>",""),paramValueList.get(0).replace("<dd>","").replace("</dd>",""));
                    }
                }
            }
            return map;
    }
    //正则表达式解析字符串的通用方法
    public static List<String> getStrArrayByReg(String orgStr,String pattern){
            List<String> result=new ArrayList<String>();
            Pattern compile = Pattern.compile(pattern);
            Matcher m=compile.matcher(orgStr);
            //通用规格参数数据
            while (m.find()) {
                String targetStr = m.group();
                result.add(targetStr);
            }
            return result;
    }

    //提取字符串中的数字
    public static Map<String,Object> getNumberFromStr(String str){
        List<String> listNumber=new ArrayList<String>();
        List<String> listStr=new ArrayList<String>();
        Pattern pattern=Pattern.compile("\\d+");
        Matcher matcher=pattern.matcher(str);
        while (matcher.find()){
            listNumber.add(matcher.group());
        }
        Pattern patternStr=Pattern.compile("\\D+");
        matcher=patternStr.matcher(str);
        while (matcher.find()){
            listStr.add(matcher.group());
        }
        Map<String,Object> map=new HashMap<>();
        map.put("listNumber",listNumber);
        map.put("listStr",listStr);
        return map;
    }
}
